package user_view

import (
	"gin_framework/global"
	"gin_framework/models"
	"gin_framework/models/res"
	ctype "gin_framework/models/type"
	"gin_framework/service/common"
	"gin_framework/utils/desense"
	"gin_framework/utils/jwts"
	"github.com/gin-gonic/gin"
)

type UserRequest struct {
	Name     string `json:"name"  structs:"name"`
	Password string `json:"password" structs:"password"`
	Phone    string `json:"phone" structs:"phone" valid:"matches(^1[3-9]{1}\\d{9}$)`
	Email    string `json:"email" structs:"email" valid:"email"`
}

func UserListView(c *gin.Context) {
	_claims, _ := c.Get("claims")
	claims := _claims.(*jwts.CustomClaims)
	var page models.PageInfo
	if err := c.ShouldBindJSON(&page); err != nil {
		res.FailWithMsg("参数绑定错误", c)
		return
	}

	list, count, err := common.ComList(models.UserModel{}, common.Option{
		PageInfo: page,
		Debug:    true,
	})
	if err != nil {
		global.Log.Error(err)
		res.FailWithMsg("查询数据错误", c)
		return
	}

	var users []models.UserModel
	for _, user := range list {
		if claims.Role != ctype.PermissionAdmin {
			// 不是管理员
			user.UserName = ""
		}
		user.Email = desense.DesensitizationEmail(user.Email)
		user.Tel = desense.DesensitizationTel(user.Tel)
		users = append(users, user)
	}
	res.OkWithList(users, count, c)
}
